var schoolListUrl = basePath + "/school/json/query";
var schoolDisUrl = basePath + "/school/dis/course/query";
var schoolAddAndUpdateUrl = basePath + "/school/dis/course/addAndUpdate";

var dataGrid;
$(function() {
	loadData(null);
	initSchool();
});

function loadData(schoolId) {
	dataGrid = $('#schoolDisManageDg').datagrid({
		url : schoolDisUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		idField : 'id',
		autoRowHeight : 140,
		queryParams: {schoolId: schoolId },
		singleSelect: true,
		nowrap : true,
		striped : true,
		rownumbers : true,
		onClickCell: onClickCell,
		columns : [ [ {
			field : 'courseId',
			title : '操作',
			align : 'center',
			checkbox : true
		}, {
			field : 'schoolCity',
			title : '学校所在城市',
			align : "center",
			resizable : true,
			formatter : function(value) {
				return dictDataFormat('city', $("#schoolCity").val());
			}
		}, {
			field : 'schoolName',
			title : '学校名称',
			align : "center",
			resizable : true,
			formatter : function() {
				var obj = document.getElementById("schoolName");
				return obj.options[obj.selectedIndex].text;
			}
		}, {
			field : 'courseCity',
			title : '课程城市',
			align : "center",
			resizable : true,
			formatter : function(value) {
				return dictDataFormat('city', value);
			}
		}, {
			field : 'couseName',
			title : '课程名称',
			align : "center",
			resizable : true
		}, {
			field : 'onlinePrice',
			title : '线上价格',
			align : "center",
			resizable : true
		}, {
			field : 'schoolPrice',
			title : '学校价格',
			align : "center",
			resizable : true
		}, {
			field : 'secondSchoolPrice',
			title : '学校第二次折扣价',
			align : "center",
			resizable : true,
			editor : "numberbox"
		}] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(!(!$.isEmptyObject(changes) && changes.secondSchoolPrice.length > 0)) {return;}
			changes.id = rowData.id;
			changes.schoolPrice = changes.secondSchoolPrice;
			changes.schoolId = $("#schoolName").val();
			changes.courseId = rowData.courseId;
			addmodify(changes)
		}
	});
}

var schoolData = null;
function initSchool() {
	$.ajax({
	   type: "POST",
	   url: schoolListUrl,
	   data:{
		   rows: -1
	   },
	   success: function(data){
		   if(schoolData == null) {
			   schoolCompent(data.rows);
		   }
		   schoolData = data.rows;   
	   }
	});
}

function loadSchool(record){
	var list = new Array();
	var v = record.dictDataKey;
	for(var i = 0; i < schoolData.length; i++) {
		if(schoolData[i].city == v) {
			list.push(schoolData[i]);
		}
	}
	schoolCompent(list);
}

function schoolCompent(data) {
	var html = "<option value=\"\">请选择</option>";
	for(var i = 0; i < data.length; i++) {
		html += "<option value=\"" + data[i].id +"\">" + data[i].schoolName + "</option>";
	}
    $("#schoolName").html(html);
}






function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	var obj = {};
	obj.schoolId = $("#schoolName").val();
	dataGrid.datagrid('load', obj);
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function save(id) {
	if(dgEdit[id] != null){
		$('#' + id).datagrid('endEdit', dgEdit[id]);
		dgEdit[id] = null;
	}
}

function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: schoolAddAndUpdateUrl,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
		   }else{
			   $.messager.alert('提示', '变更失败', 'error');
		   }
		}
	});
}

function getSchoolCity(thiz) {
	var schoolId = $(thiz).val();
	for(var i = 0; i < schoolData.length; i++) {
		if(schoolData[i].schoolId = schoolId) {
			$("#schoolCity").val(schoolData[i].city);
			break;
		}
	}
}

$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$(this).datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if (dataGrid.datagrid('validateRow', editIndex)){
		dataGrid.datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickCell(index, field){
	if (endEditing()){
		dataGrid.datagrid('selectRow', index)
				.datagrid('editCell', {index:index,field:field});
		editIndex = index;
	}
}